﻿/*  Groep 1:
*     János de Vries
*     Pelle Kersaan
*     Alex Aalbertsberg
*/ 

using System;

namespace SortOf
{
    public class BinaryTree<T> where T : IComparable
    {
        public BinaryTreeNode<T> root { get; set; }

        /// <summary>
        /// Construct the BinaryTree and sets the root to null.
        /// </summary>
        public BinaryTree()
        {
            root = null;
        }

        /// <summary>
        /// Inserts data into the BinaryTree
        /// </summary>
        /// <param name="data">The data to insert</param>
        public virtual void insert(T data)
        {
            BinaryTreeNode<T> newNode = new BinaryTreeNode<T>(data);

            if (root == null)
            {
                root = newNode;
            }
            else
            {
                BinaryTreeNode<T> current = root;
                BinaryTreeNode<T> parent;
                while (true)
                {
                    parent = current;
                    if (data.CompareTo(current.data) < 0)
                    {
                        current = current.left;
                        if (current == null)
                        {
                            parent.left = newNode;
                            break;
                        }
                    }
                    else
                    {
                        current = current.right;
                        if (current == null)
                        {
                            parent.right = newNode;
                            break;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Prints the BinaryTree
        /// </summary>
        public void print()
        {
            String offset = "";
            printNode(root, ref offset);
        }

        /// <summary>
        /// Used internaly to print one node.
        /// </summary>
        /// <param name="node">The node to print</param>
        /// <param name="offset">The whitespace offset on the left in the writeline.</param>
        private void printNode(BinaryTreeNode<T> node, ref String offset)
        {
            Console.WriteLine(offset + node.data.ToString());

            if (node.left != null && node.right != null)
            {
                Console.WriteLine(offset + "├─┐");
                offset += "│ ";
                printNode(node.right, ref offset);
                Console.WriteLine(offset);
                offset = offset.Substring(0, offset.Length - 2);
                printNode(node.left, ref offset);
            }
            else if (node.right != null)
            {
                Console.WriteLine(offset + "└─┐");
                offset += "  ";
                printNode(node.right, ref offset);
                offset = offset.Substring(0, offset.Length - 2);
            }
            else if (node.left != null)
            {
                Console.WriteLine(offset + "│");
                printNode(node.left, ref offset);
            }
        }
    }
}
